﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 学生选课及成绩查询管理系统
{
    public partial class addStudentScoreInfo : Form
    {
        public addStudentScoreInfo()
        {
            InitializeComponent();
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string stuno = txt_stuno.Text;
            string cno = txt_cno.Text;
            string score = txt_score.Text;
            if (string.IsNullOrEmpty(stuno) || string.IsNullOrEmpty(cno) || string.IsNullOrEmpty(score))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }

            string sql = "insert into grade(stuno,cno,score) values(@stuno,@cno,@score)";
            SqlParameter[] sqlarray = { new SqlParameter("@stuno",stuno),
                                          new SqlParameter("@cno",cno),new SqlParameter("@score",score)};
            int res = DBAccess.GetState(sql, sqlarray);
            if (res != 0)
            {
                MessageBox.Show("成功录入学生成绩！");
                addStudentScoreInfo_Load(sender,e);
            }
            else
            {
                MessageBox.Show("录入失败");
            }
        }

        private void txt_classno_Click(object sender, EventArgs e)
        {
            
        }

        private void txt_score_Click(object sender, EventArgs e)
        {
            string cno = txt_cno.Text;
            if (cno == "")
            {
                return;
            }
            string sql = "select count(*) from course where cno=" + cno;
            object res = DBAccess.GetOne(sql);
            if ((int)res == 0)
            {
                MessageBox.Show("没有该课程！请核对后再填写");
                txt_cno.Focus();
                return;
            }
        }

        private void txt_cno_Click(object sender, EventArgs e)
        {
            string stuno = txt_stuno.Text;
            if (stuno == "")
            {
                return;
            }
            string sql = "select count(*) from student where stuno=" + stuno;
            object res = DBAccess.GetOne(sql);
            if ((int)res == 0)
            {
                MessageBox.Show("没有该学生！请核对后再填写");
                txt_stuno.Focus();
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addStudentScoreInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from grade";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_stuno.Text = dataGridView1.Rows[row - 1].Cells[0].Value.ToString();
            txt_cno.Text = dataGridView1.Rows[row - 1].Cells[1].Value.ToString();
            txt_score.Text = dataGridView1.Rows[row - 1].Cells[2].Value.ToString();
            
        }
    }
}
